package com.fasttreatment.zzr.demo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.androidlibrary.base.BaseFragment;
import com.android.androidlibrary.imageloader.ILoader;
import com.android.androidlibrary.imageloader.ImageFactory;
import com.android.androidlibrary.router.Router;
import com.fasttreatment.zzr.demo.R;
import com.fasttreatment.zzr.demo.myrouter.UrlRouter;
import com.fasttreatment.zzr.demo.view.DividerLinearLayout;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;


import butterknife.BindView;
import butterknife.ButterKnife;

import static com.fasttreatment.zzr.demo.R.id.ec_edit_chat_id;

/**
 * Created by zzr on 17/1/11.
 */

public class Test3Fragment extends BaseFragment {
    @BindView(R.id.dl_layout)
    DividerLinearLayout mDlLayout;
    @BindView(R.id.iv_login)
    ImageView mIvLogin;
    @BindView(ec_edit_chat_id)
    EditText mEcEditChatId;
    @BindView(R.id.btn_start_chat)
    Button mBtnStartChat;
    @BindView(R.id.btn_sign_out)
    Button mBtnSignOut;

    @Override
    public void initData(Bundle savedInstanceState) {

        initView();
    }

    @Override
    public void setListener() {
        mIvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UrlRouter.goLogin(context);
            }
        });
        mBtnSignOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signOut();
            }
        });
        mBtnStartChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取我们发起聊天的者的username
                String chatId = mEcEditChatId.getText().toString().trim();
                if (!TextUtils.isEmpty(chatId)) {
                    // 获取当前登录用户的 username
                    String currUsername = EMClient.getInstance().getCurrentUser();
                    if (chatId.equals(currUsername)) {
                        Toast.makeText(context, "不能和自己聊天", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    UrlRouter.goChat(context, chatId);
                } else {
                    Toast.makeText(context, "Username 不能为空", Toast.LENGTH_LONG).show();
                }

            }
        });


    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_test3;
    }


    public void initView() {
        ImageFactory.getLoader().loadResource(mIvLogin, R.drawable.logo, new ILoader.Options(R.mipmap.ic_launcher, R.mipmap.ic_launcher, 1));
//        mDlLayout.setDividerColor(Color.parseColor("#4a4a4a"));
//        mDlLayout.setPadding(20, 0, 20, 0);
//        mDlLayout.setFooterDividerPadding(10, 10);
//        mDlLayout.setHeaderDividerPadding(10, 10);
    }

    /**
     * 退出登录
     */
    private void signOut() {
        // 调用sdk的退出登录方法，第一个参数表示是否解绑推送的token，没有使用推送或者被踢都要传false
        EMClient.getInstance().logout(false, new EMCallBack() {
            @Override
            public void onSuccess() {
                Log.i("zzr", "logout success");
                // 调用退出成功，结束app
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "帐号已退出", Toast.LENGTH_SHORT).show();
                    }
                });

            }

            @Override
            public void onError(int i, String s) {
                Log.i("zze", "logout error " + i + " - " + s);
            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }

}
